# -*- coding: utf-8 -*-
import random
import sys
from PyQt5 import QtCore, QtWidgets
from OpenGL.GL import *
from OpenGL.GLU import gluPerspective


class GLWidget(QtWidgets.QOpenGLWidget):
    def __init__(self):
        super().__init__()
        self.x_rotation = 0
        self.y_rotation = 0
        self.z_rotation = 0

    def initializeGL(self):
        glEnable(GL_DEPTH_TEST)
        glEnable(GL_BLEND)
        glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA)

    def resizeGL(self, width, height):
        glViewport(0, 0, width, height)
        glMatrixMode(GL_PROJECTION)
        glLoadIdentity()
        gluPerspective(45, (width / height) if height != 0 else 1, 0.1, 100.0)
        glMatrixMode(GL_MODELVIEW)

    def paintGL(self):
        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)
        glLoadIdentity()
        glTranslatef(0.0, 0.0, -5.0)
        glRotatef(self.x_rotation, 1, 0, 0)
        glRotatef(self.y_rotation, 0, 1, 0)
        glRotatef(self.z_rotation, 0, 0, 1)

        self.drawCubeWithColors()
        self.drawAxes()

    def drawCubeWithColors(self):
        glBegin(GL_QUADS)

        # Back face (半透明绿色)
        glColor4f(0, 1, 0, 0.5)
        glVertex3f(-1, -1, -1)
        glVertex3f(-1, 1, -1)
        glVertex3f(1, 1, -1)
        glVertex3f(1, -1, -1)

        # Front face (半透明红色)
        glColor4f(1, 0, 0, 0.5)
        glVertex3f(-1, -1, 1)
        glVertex3f(1, -1, 1)
        glVertex3f(1, 1, 1)
        glVertex3f(-1, 1, 1)

        # Left face (半透明蓝色)
        glColor4f(0, 0, 1, 0.5)
        glVertex3f(-1, -1, -1)
        glVertex3f(-1, -1, 1)
        glVertex3f(-1, 1, 1)
        glVertex3f(-1, 1, -1)

        # Right face (半透明黄色)
        glColor4f(1, 1, 0, 0.5)
        glVertex3f(1, -1, -1)
        glVertex3f(1, -1, 1)
        glVertex3f(1, 1, 1)
        glVertex3f(1, 1, -1)

        # Top face (半透明紫色)
        glColor4f(1, 0, 1, 0.5)
        glVertex3f(-1, 1, -1)
        glVertex3f(1, 1, -1)
        glVertex3f(1, 1, 1)
        glVertex3f(-1, 1, 1)

        # Bottom face (半透明青色)
        glColor4f(0, 1, 1, 0.5)
        glVertex3f(-1, -1, -1)
        glVertex3f(-1, -1, 1)
        glVertex3f(1, -1, 1)
        glVertex3f(1, -1, -1)

        glEnd()

        # 绘制立方体的边缘
        glBegin(GL_LINES)
        glColor3f(1, 1, 1)  # 线框颜色为白色
        vertices = [
            (-1, -1, 1), (1, -1, 1),  # Front bottom
            (1, -1, 1), (1, 1, 1),  # Front right
            (1, 1, 1), (-1, 1, 1),  # Front top
            (-1, 1, 1), (-1, -1, 1),  # Front left
            (-1, -1, -1), (1, -1, -1),  # Back bottom
            (1, -1, -1), (1, 1, -1),  # Back right
            (1, 1, -1), (-1, 1, -1),  # Back top
            (-1, 1, -1), (-1, -1, -1),  # Back left
            (-1, -1, 1), (-1, -1, -1),  # Left
            (1, -1, 1), (1, -1, -1),  # Right
            (-1, 1, 1), (-1, 1, -1),  # Top
            (1, 1, 1), (1, 1, -1)  # Bottom
        ]

        for vertex in vertices:
            glVertex3f(*vertex)

        glEnd()

    def drawAxes(self):
        # 绘制坐标轴并添加箭头
        arrow_size = 0.1

        glBegin(GL_LINES)

        # X轴 (红色)
        glColor3f(1, 0, 0)
        glVertex3f(-2, 0, 0)
        glVertex3f(2, 0, 0)
        glEnd()

        # 箭头
        glBegin(GL_TRIANGLES)
        glVertex3f(2, 0, 0)  # 尖端
        glVertex3f(1.9, 0.1, 0)  # 箭头左
        glVertex3f(1.9, -0.1, 0)  # 箭头右
        glEnd()

        glBegin(GL_LINES)
        # Y轴 (绿色)
        glColor3f(0, 1, 0)
        glVertex3f(0, -2, 0)
        glVertex3f(0, 2, 0)
        glEnd()

        # 箭头
        glBegin(GL_TRIANGLES)
        glVertex3f(0, 2, 0)  # 尖端
        glVertex3f(0.1, 1.9, 0)  # 箭头左
        glVertex3f(-0.1, 1.9, 0)  # 箭头右
        glEnd()

        glBegin(GL_LINES)
        # Z轴 (蓝色)
        glColor3f(0, 0, 1)
        glVertex3f(0, 0, -2)
        glVertex3f(0, 0, 2)
        glEnd()

        # 箭头
        glBegin(GL_TRIANGLES)
        glVertex3f(0, 0, 2)  # 尖端
        glVertex3f(0.1, 0, 1.9)  # 箭头左
        glVertex3f(-0.1, 0, 1.9)  # 箭头右
        glEnd()

    def setXRotation(self, x_angle):
        self.x_rotation = x_angle
        self.update()

    def setYRotation(self, y_angle):
        self.y_rotation = y_angle
        self.update()

    def setZRotation(self, z_angle):
        self.z_rotation = z_angle
        self.update()

    def resetRotation(self):
        self.x_rotation = 0
        self.y_rotation = 0
        self.z_rotation = 0
        self.update()


class GyroWidget(QtWidgets.QWidget):
    gyroResultSignal = QtCore.pyqtSignal(int)

    def __init__(self, parent=None):
        super(GyroWidget, self).__init__(parent)
        self.glWidget = GLWidget()

        self.xDial, self.xLabel = self.createDialLabel("X:", self.glWidget.setXRotation)
        self.yDial, self.yLabel = self.createDialLabel("Y:", self.glWidget.setYRotation)
        self.zDial, self.zLabel = self.createDialLabel("Z:", self.glWidget.setZRotation)

        resetButton = QtWidgets.QPushButton("Reset Rotation")
        resetButton.clicked.connect(self.ResetRotation)  # 连接按钮事件

        mainLayout = QtWidgets.QVBoxLayout()
        mainLayout.addWidget(self.glWidget)

        dialLayout = QtWidgets.QHBoxLayout()
        dialLayout.addLayout(self.createDialLayout(self.xDial, self.xLabel))
        dialLayout.addLayout(self.createDialLayout(self.yDial, self.yLabel))
        dialLayout.addLayout(self.createDialLayout(self.zDial, self.zLabel))

        mainLayout.addLayout(dialLayout)

        mainLayout.addWidget(resetButton)  # 添加按钮到布局
        self.setLayout(mainLayout)

        self.xDial.setValue(0)
        self.yDial.setValue(0)
        self.zDial.setValue(0)

        # self.timer = QtCore.QTimer(self)
        # self.timer.timeout.connect(self.randomFlip)
        # self.timer.start(1000)

        self.setWindowTitle("3D Rotation Example")

    def createDialLabel(self, label_text, slot):
        dial = QtWidgets.QDial()
        dial.setRange(-18000, 18000)
        dial.valueChanged.connect(lambda value: slot(value / 100.00))

        label = QtWidgets.QLabel(label_text + " 0.00")
        dial.valueChanged.connect(lambda value: label.setText(f"{label_text} {value / 100.00:.2f}"))
        dial.setFixedSize(88, 88)
        label.setStyleSheet("font-size: 33px; font-weight: bold;")
        label.setAlignment(QtCore.Qt.AlignmentFlag.AlignCenter)
        return dial, label

    def createDialLayout(self, dial, label):
        layout = QtWidgets.QHBoxLayout()
        layout.addWidget(label)
        layout.addWidget(dial)
        return layout

    def ResetRotation(self):
        self.UpdateUI(0, 0, 0)
        self.glWidget.resetRotation()

    def UpdateUI(self, x_angle, y_angle, z_angle):
        self.xDial.setValue(int(x_angle * 100))
        self.yDial.setValue(int(y_angle * 100))
        self.zDial.setValue(int(z_angle * 100))
        self.glWidget.setXRotation(x_angle)
        self.glWidget.setYRotation(y_angle)
        self.glWidget.setZRotation(z_angle)

    def randomFlip(self):
        random_x = random.uniform(-180, 180)  # 随机角度
        random_y = random.uniform(-180, 180)
        random_z = random.uniform(-180, 180)
        self.UpdateUI(random_x, random_y, random_z)

    def gyroDataHandle(self, gyro):
        # print(gyro)
        words = gyro.split(",")
        myDegreeX = int(words[0]) / 2 - 100
        myDegreeY = int(words[1]) / 2 - 100
        myDegreeZ = int(words[2]) / 2 - 100
        print(f"{myDegreeX},{myDegreeY},{myDegreeZ}")
        self.UpdateUI(myDegreeX, myDegreeY, myDegreeZ)


if __name__ == "__main__":
    app = QtWidgets.QApplication(sys.argv)
    window = GyroWidget()
    window.resize(800, 600)
    window.show()
    sys.exit(app.exec_())
